package com.novunda.platform.repositories;

import com.novunda.platform.common.repositories.BaseRepository;
import com.novunda.platform.entities.Menu;
import com.novunda.platform.entities.base.BaseEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author linfeng
 */
public interface MenuRepository extends BaseRepository<Menu, Long> {

    @Query("from Menu where delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' order by id")
    List<Menu> findAllList();


    @Modifying
    @Query("update Menu set delFlag='" + BaseEntity.DEL_FLAG_DELETE + "' where id = ?1 or parentIds like ?2")
    int deleteById(Long id, String likeParentIds);

    List<Menu> findByParentIdsLike(String parentIds);


    @Query("from Menu where delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and isShow='" + BaseEntity.SHOW + "' order by id")
    List<Menu> findAllShowList();

    @Query("from Menu where delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and isShow='" + BaseEntity.SHOW + "' and parent.id='1' order by id")
    List<Menu> findRootShowList();

    @Query("from Menu where delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and isShow='" + BaseEntity.SHOW
            + "' and shortcut='" + BaseEntity.SHORTCUT_YES + "' order by id")
    List<Menu> findShortcutList();

    @Query("select distinct m from Menu m, Role r, User u where m in elements (r.menuList) and r in elements (u.roleList)" +
            " and m.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and r.delFlag='" + BaseEntity.DEL_FLAG_NORMAL +
            "' and u.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and u.id=?1" + // or (m.user.id=?1  and m.delFlag='" + Menu.DEL_FLAG_NORMAL + "')" +
            " order by m.id")
    List<Menu> findByUserId(Long userId);

    @Query("select distinct m from Menu m, Role r, User u where m in elements (r.menuList) and r in elements (u.roleList)" +
            " and m.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and r.delFlag='" + BaseEntity.DEL_FLAG_NORMAL +
            "' and u.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and u.id=?1 and m.isShow='" + BaseEntity.SHOW +
            "' order by m.id")
    List<Menu> findShowByUserId(Long userId);

    @Query("select distinct m from Menu m, Role r, User u where m in elements (r.menuList) and r in elements (u.roleList)" +
            " and m.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and r.delFlag='" + BaseEntity.DEL_FLAG_NORMAL +
            "' and u.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and u.id=?1 and m.isShow='" + BaseEntity.SHOW +
            "' and m.parent.id='1' order by m.id")
    List<Menu> findRootShowByUserId(Long userId);

    @Query("select distinct m from Menu m, Role r, User u where m in elements (r.menuList) and r in elements (u.roleList)" +
            " and m.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and r.delFlag='" + BaseEntity.DEL_FLAG_NORMAL +
            "' and u.delFlag='" + BaseEntity.DEL_FLAG_NORMAL + "' and u.id=?1 and m.isShow='" + BaseEntity.SHOW +
            "' and m.shortcut='" + BaseEntity.SHORTCUT_YES + "' order by m.id")
    List<Menu> findShortcutByUserId(Long userId);
    
    @Query("from Menu where delFlag='" + BaseEntity.DEL_FLAG_NORMAL + 
            "' and parentIds like ?1 and name = ?2 and permission = ?3")
    Menu findByParentsIdAndNameAndPermission(String id, String name, String permisson);
    
    @Query("from Menu where delFlag='" + BaseEntity.DEL_FLAG_NORMAL + 
            "' and name = ?1")
    Menu findByName(String name);

}
